<template>
	<view class="page-container">
		<view class="nav-bar">
			<navigator class="back-btn" open-type="navigateBack" delta="1">←</navigator>
			<view class="page-title">修改个人信息</view>
		</view>

		<view class="MainBox">
			<view class="avatar-box">
				<image class="img" :src="avatarUrl" mode="aspectFill"></image>
				<view class="edit-tip" @click="chooseAvatar">更换头像</view>
			</view>

			<view class="form-list">
				<view class="form-item">
					<view class="label">姓名</view>
					<input class="input" v-model="userInfo.name" placeholder="请输入姓名" />
				</view>

				<view class="form-item">
					<view class="label">手机号码</view>
					<input class="input" v-model="userInfo.phone" placeholder="请输入手机号码" type="number" maxlength="11" />
				</view>

				<view class="form-item">
					<view class="label">身份证号</view>
					<input class="input" v-model="userInfo.idCard" placeholder="请输入身份证号" maxlength="18" />
				</view>

				<view class="form-item">
					<view class="label">邮箱</view>
					<input class="input" v-model="userInfo.email" placeholder="请输入邮箱" type="email" />
				</view>

				<view class="form-item">
					<view class="label">地区</view>
					<input class="input" v-model="userInfo.region" placeholder="请输入地区" />
				</view>
			</view>
		</view>

		<button class="save-btn" @click="saveInfo">保存修改</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatarUrl: '/static/images/avatar-default.png',
				userInfo: {
					name: '张**',
					phone: '13300001111',
					idCard: '12130000434',
					email: '13300001111@qq.com',
					region: '四川'
				}
			};
		},
		onLoad() {
			const pages = getCurrentPages();
			const prevPage = pages[pages.length - 2];
			if (prevPage.data.userInfo) {
				this.userInfo = {
					...prevPage.data.userInfo
				};
			}
		},
		methods: {
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.avatarUrl = res.tempFilePaths[0];
					}
				});
			},
			saveInfo() {
				if (!this.userInfo.name.trim()) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					});
					return;
				}
				if (!this.userInfo.phone.trim() || this.userInfo.phone.length !== 11) {
					uni.showToast({
						title: '请输入正确的手机号码',
						icon: 'none'
					});
					return;
				}

				uni.showToast({
					title: '保存成功',
					icon: 'success'
				});

				setTimeout(() => {
					const pages = getCurrentPages();
					const prevPage = pages[pages.length - 2];
					prevPage.setData({
						userInfo: this.userInfo,
						avatarUrl: this.avatarUrl
					});
					uni.navigateBack({
						delta: 1
					});
				}, 1000);
			}
		}
	};
</script>

<style scoped>
	.page-container {
		background-color: #fff8f0;
		min-height: 100vh;
		padding-bottom: 30px;
	}

	.nav-bar {
		height: 44px;
		background-color: #fff8f0;
		display: flex;
		align-items: center;
		padding: 0 15px;
		border-bottom: 1px solid #ffd5b8;
	}

	.back-btn {
		font-size: 20px;
		color: #ff7d00;
	}

	.page-title {
		font-size: 18px;
		font-weight: 600;
		color: #ff7d00;
		margin-left: 10px;
	}

	.MainBox {
		background-color: #fff;
		border-radius: 12px;
		padding: 20px 15px;
		margin: 15px;
		box-shadow: 0 2px 8px rgba(255, 125, 0, 0.08);
	}

	.avatar-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 25px;
	}

	.img {
		width: 80px;
		height: 80px;
		border-radius: 50%;
		border: 2px solid #ffd5b8;
		margin-bottom: 8px;
	}

	.edit-tip {
		font-size: 12px;
		color: #ff9a6c;
		cursor: pointer;
	}

	.form-list {
		margin-top: 10px;
	}

	.form-item {
		display: flex;
		align-items: center;
		padding: 12px 0;
		border-bottom: 1px solid #fff8f0;
	}

	.form-item:last-child {
		border-bottom: none;
	}

	.label {
		font-size: 15px;
		color: #666;
		width: 80px;
	}

	.input {
		font-size: 15px;
		color: #333;
		flex: 1;
		padding: 5px 0;
		border: none;
		outline: none;
	}

	.input::placeholder {
		color: #e0a872;
		font-size: 14px;
	}

	.save-btn {
		width: calc(100% - 30px);
		margin: 0 auto;
		padding: 14px 0;
		background-color: #ff7d00;
		color: #fff;
		border: none;
		border-radius: 12px;
		font-size: 16px;
		box-shadow: 0 4px 12px rgba(255, 125, 0, 0.15);
	}

	.save-btn:active {
		opacity: 0.9;
		transform: scale(0.98);
	}
</style>